package function;

import java.util.UUID;
import java.util.function.*;

public class FunctionTest {

	public static void main(String[] args) {
		//Function定义有一个参数的方法
		Function<Integer, Integer> add1 = x -> x+1;
		Function<Integer, Integer> add_x = add1.andThen(x -> x*10);
		System.out.println(add1.apply(5));
		System.out.println(add_x.apply(6));
		Function<String, String> append = s1 -> s1 + " test";
		System.out.println(append.apply("hello"));
		
		System.out.println("------------");
		//BiFunction定义有两个参数的方法
		BiFunction<Integer, Integer, Integer> intAdd = (x,y) -> x+y;
		BiFunction<Integer, Integer, Integer> intAdd_x = intAdd.andThen(sum -> sum*10);
		System.out.println(intAdd.apply(10, 6));
		System.out.println(intAdd_x.apply(10, 6));
		BiFunction<String, String, String> strAdd = (x,y) -> x+y;
		System.out.println(strAdd.apply("hello"," world"));
		
		System.out.println("------------");
		//Consumer一个输入，没输出 
		Consumer<Integer> c = x -> {
			x++;
			System.out.println(x);
		};
		Consumer<Integer> c2 = c.andThen(x-> {
			x +=10;
			System.out.println(x);
		});
		c.accept(100);
		c2.accept(100);
		
		System.out.println("------------");
		//Supplier，没输入，有输出
		Supplier<String> supplier = () -> UUID.randomUUID().toString();
		System.out.println(supplier.get());
		
		System.out.println("------------");
		//Predicate，输入一个参数，输出boolean值
		Predicate<String> predicate = s -> s !=null && s.startsWith("xu");
		Predicate<String> predicate2 = s -> s !=null && s.endsWith("xin");
		System.out.println(predicate.test("xx"));
		System.out.println(predicate.test("xuyx"));
		System.out.println(predicate2.test("xuyx"));
		System.out.println(predicate2.test("xuyxin"));
	}

}
